package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/member")
public class MemberController {
    @Reference
    private MemberService memberService;


        /**
         * 新增
         *
         */

        @PostMapping(path = "/add")
        public Result add(@RequestBody Member member) {

        try {
            memberService.add(member);
            return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MEMBER_FAIL);
        }
    }

        /**
         * 分页查询检查项数据
         *
         * @param queryPageBean
         * @return
         */
        @GetMapping(path = "/findPage")
        public Result findPage(QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = memberService.pageQuery(queryPageBean);
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS, pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MEMBER_FAIL);
        }
    }

        /**
         * 根据id删除检查项
         *
         * @param id
         * @return
         */
        @DeleteMapping(path = "/{id}")
        public Result deleteById(@PathVariable Integer id) {
        try {
            //删除检查项
            memberService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_MEMBER_SUCCESS);
        } catch (RuntimeException e) {
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MEMBER_FAIL);
        }
    }

//        /**
//         * 根据id查询数据详情
//         *
//         * @param id
//         * @return
//         */
        @GetMapping(path = "/{id}")
        public Result findById(@PathVariable int id) {
        try {
            //查询
            Member member = memberService.findById(id);
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS, member);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MEMBER_FAIL);
        }
    }
//
//        /**
//         * 确定编辑, 更新数据
//         *
//         * @param member
//         * @return
//         */
        @PutMapping(path = "/edit")
        public Result edit(@RequestBody Member member) {
            try {
                memberService.edit(member);
                return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);
            } catch (Exception e) {
                e.printStackTrace();
                return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
            }
        }

    /**
     * 查询所有数据
     *
     * @return
     */
    @GetMapping(path = "/findAll")
    public Result findAll() {
        try {
            List<Member> items = memberService.findAll();
            return new Result(true, MessageConstant.QUERY_MEMBER_SUCCESS, items);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_MEMBER_FAIL);
        }
    }
//    public void Excel() {
//        //添加Newtonsoft.JSon引用
//        string rowe = Request["data"];
//        var list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Excelrghgh>>(rowe); //建立名称(随意)为Excelrghgh的实体类，Code,Naem,Address
//        //JArray ja = (JArray)JsonConvert.DeserializeObject(rowe);
//        //var list = ja.ToObject<List<Excelrghgh>>();
//        //两种方式将数据反序列化
//        //拼接表头循环数据
//        var table = new StringBuilder();
//        table.Append("<table border='1'><tr><td>编号</td><td>名称</td><td>地址</td></tr>");
//        foreach (var item in list)
//        {
//            table.Append("<tr>");
//            table.Append("<td>"+item.Code +"</td>");
//            table.Append("<td>" + item.Name+ "</td>");
//            table.Append("<td>" + item.Address + "</td>");
//            table.Append("</tr>");
//        }
//        table.Append("</table>");
//        var listData = table.ToString();
//        Response.Clear();
//        Response.ContentType = "applicationvnd.ms-excel";
//        //导出excel格式
//        Response.AddHeader(
//        @"Content-Disposition",
//            string.Format(@"attachment; filename=""{0}.xls", HttpUtility.UrlPathEncode("表_" + DateTime.Now.ToString("yyyy-MM-dd HH：mm：ss"))));
//        Response.Write(listData);
//    }
}



